package com.io;

import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

/**
 * The class/interface
 *
 * @author guodd
 * @version 1.0 use jdk 1.8
 */
public class DateUtil {
    /**
     * 获取两个日期间隔的所有日期
     *
     * @param start 格式必须为'2018-01-25'
     * @param end   格式必须为'2018-01-25'
     * @return 时间区间集合
     */
    public static List<String> getBetweenDate(String start, String end, long week) {
        List<String> list = new ArrayList<>();
        LocalDate startDate = LocalDate.parse(start);
        LocalDate endDate = LocalDate.parse(end);
        long info = ChronoUnit.MONTHS.between(startDate, endDate);
        if (week != 1) {
            info = week / 2;
        }
        if (info < 1) {
            return list;
        }
        Stream.iterate(startDate, d -> d.plusMonths(week))
                .limit(info + 1)
                .forEach(f -> list.add(f.toString()));
        return list;
    }

    public static void main(String[] args) {
        List<String> betweenDate = getBetweenDate("2020-01-02", "2020-04-01", 1);
        for (String s : betweenDate) {
            System.out.println(s);
        }
    }
}
